package com.example.first_examination;

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.first_examination.Fragment.Fragment1;
import com.example.first_examination.Fragment.Fragment2;
import com.example.first_examination.Fragment.HomeFragment;
import com.example.first_examination.Fragment.MyFragment;
import com.google.android.material.tabs.TabLayout;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.ll)
    LinearLayout ll;
    @BindView(R.id.tab)
    TabLayout tab;
    @BindView(R.id.tv_toolbar)
    TextView tvToolbar;
    private FragmentManager supportFragmentManager;
    private FragmentTransaction transaction;
    private HomeFragment homeFragment;
    private MyFragment myFragment;
    private Fragment1 fragment1;
    private Fragment2 fragment2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
        initTab();
    }

    private void initTab() {
        supportFragmentManager = getSupportFragmentManager();
        transaction = supportFragmentManager.beginTransaction();
        homeFragment = new HomeFragment();
        myFragment = new MyFragment();
        fragment1 = new Fragment1();
        fragment2 = new Fragment2();
        transaction.add(R.id.ll, homeFragment);
        transaction.add(R.id.ll, myFragment);
        transaction.add(R.id.ll, fragment1);
        transaction.add(R.id.ll, fragment2);
        transaction.show(homeFragment).hide(myFragment).hide(fragment1).hide(fragment2).commit();

        tab.addTab(tab.newTab().setText("首页").setIcon(R.drawable.selector1));
        tab.addTab(tab.newTab().setText("广场").setIcon(R.drawable.selector1));
        tab.addTab(tab.newTab().setText("公众号").setIcon(R.drawable.selector1));
        tab.addTab(tab.newTab().setText("体系").setIcon(R.drawable.selector1));

        tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

            private FragmentTransaction transaction;

            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                transaction = supportFragmentManager.beginTransaction();
                switch (tab.getPosition()) {
                    case 0:
                        transaction.show(homeFragment).hide(myFragment).hide(fragment1).hide(fragment2);
                        tvToolbar.setText(tab.getText());
                        break;
                    case 1:
                        transaction.show(myFragment).hide(homeFragment).hide(fragment1).hide(fragment2);
                        tvToolbar.setText(tab.getText());
                        break;
                    case 2:
                        transaction.show(fragment1).hide(myFragment).hide(homeFragment).hide(fragment2);
                        tvToolbar.setText(tab.getText());
                        break;
                    case 3:
                        transaction.show(fragment2).hide(myFragment).hide(fragment1).hide(homeFragment);
                        tvToolbar.setText(tab.getText());
                        break;
                }
                transaction.commit();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

    private void initView() {
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
    }
}
